* Construct indicator of whether the sectors in OECD IO tables are non-tradable. 
* Compute sector-level export shares from WIOD. Because WIOD and OECD group the ISIC Rev 3 2-digit sectors differently, 
* rely on a (manually constructed) concordance to map the WIOD sectors into the OECD sectors.
* Concordance is contained in concordanceWIODOECD.dta.

use "../Database/WIOD/input/wiot_full.dta", clear
drop if row_item>35 // restrict to the 35 sectors
bys year row_item row_country: egen value_temp2 = total(value) // total sales to entire world
bys year row_item row_country col_country: egen value_temp = total(value) // aggregate across uses (col_countries) for each destination
keep if row_country==col_country
keep if col_item==1 // eliminate col_items
drop col_item col_country value
sort year row_country row_item
gen domshare = value_temp / value_temp2 // share of domestic sales
gen si = 1-domshare                     // export share
rename row_country Country
drop value_temp2 value_temp

* Compute median export shares by sector
bys row_item: egen si_med = median(si)
bys row_item: gen counter=_n // eliminate origin countries and years
keep if counter==1
keep row_item si_med 

* Define a sector as NT whenever the median import share across years and countries is below 0.05
gen NT=0
replace NT = 1 if si_med<0.05
drop si_med
sort row_item

* Map the WIOD sectors to the OECD sectors, which features a different grouping of the ISIC Rev 3 2-digit sectors
expand 2 in 12 // prepare to merge with concordance
expand 2 in 14
expand 2 in 15
expand 3 in 30
sort row_item
gen concordanceid = _n
merge 1:1 concordanceid using "../Database/WIOD/input/concordanceWIODOECD.dta" // concordance between WIOD and OECD grouping of sectors
drop _merge
collapse (mean) NT, by(var) // var is an identifier for the OECD sectors
gen NTb = round(NT) // round to nearest integer (0.5 is rounded to 1)
drop NT
rename NTb NT

* prepare to merge with OECD IO table
set obs `=_N+36' // add observations to generate "import" sectors; last two rows will correspond to VA and GDP
gen var2 = _n    // new sector id
replace var2=71 if var2==69
replace var2=72 if var2==70
gen dum=0
replace dum=1 if var2>34
bys dum: gen var_temp=_n
drop dum
bys var_temp: egen NTc = total(NT) // extend the NT indicator to "import" sectors
keep var2 NTc
sort var2
rename var2 var
rename NTc NT
replace NT=2 if var==71 // this is value added
replace NT=3 if var==72 // this is GDP
save "temp/NT.dta", replace
